//
//  RegisterVC.m
//  YiShangPin
//
//  Created by yishangpin2 on 15/3/23.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "RegisterVC.h"
#import "AppDelegate.h"
#import "HomePageWaterfall.h"

@interface RegisterVC (){
    
    AppDelegate *delegate;
    NSMutableArray *picmutArray;
    int y;
    int y1;
    float AllY;
    NSTimer *timer;
}

@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    delegate = [[UIApplication sharedApplication]delegate];
    self.scrollview.contentSize = CGSizeMake(320  , 768);
    CGPoint bottomOffset = CGPointMake(self.scrollview.contentOffset.x,1);
    [self.scrollview setContentOffset:bottomOffset animated:NO];
    self.navigationController.navigationBarHidden = YES;
    picmutArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12", nil];

    [_BigButton addTarget:self action:@selector(Touchdown) forControlEvents:UIControlEventTouchDown];
    [_BigButton addTarget:self action:@selector(TouchUP) forControlEvents:UIControlEventTouchUpInside];
    [self Waterfall_Upload];
    // Do any additional setup after loading the view.
}

#pragma mark - 触摸时，停止向上滑动
-(void)Touchdown{
    [timer invalidate];
    
}
//放开时，继续滑动
-(void)TouchUP{
    [self viewDidAppear:YES];
}



- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    CGPoint bottomOffset = CGPointMake(0, 100);
    
    //设置延迟时间
    float scrollDurationInSeconds = 3.0;
    
    //计算timer间隔
    
    float totalScrollAmount = bottomOffset.y;
    float timerInterval = scrollDurationInSeconds / totalScrollAmount;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:timerInterval target:self selector:@selector(scrollScrollView:) userInfo:nil repeats:YES];
}

- (void)scrollScrollView:(NSTimer *)timer1
{
    CGPoint newScrollViewContentOffset = self.scrollview.contentOffset;
    //向上移动 1px
    newScrollViewContentOffset.y += 1;
    
    
//    newScrollViewContentOffset.y = MAX(0, newScrollViewContentOffset.y);
    
    //如果到顶了，timer中止
    if ((int)newScrollViewContentOffset.y % 400 == 0 && (int)newScrollViewContentOffset.y != 0) {
        if ([picmutArray[1]  isEqual: @"1"]) {
            NSMutableArray *array1 = [[NSMutableArray alloc] initWithObjects:@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"12",@"21",@"22", nil];
            picmutArray = array1;
        }

        else{
        NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:@"18",@"10",@"22",@"17",@"7",@"6",@"19",@"4",@"3",@"16", nil];
            picmutArray = array2;
        }
        [self Waterfall_Upload];
//        [timer invalidate];
    }
    
    //最后设置scollView's contentOffset
    self.scrollview.contentOffset = newScrollViewContentOffset;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 瀑布流排布

//瀑布流排布商品信息
- (void)Waterfall_Upload{
    //    UIImage *image = [UIImage imageNamed:picmutArray[4]];
    
    
    for (int i = 0; i<10; i++) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HomePageWaterfall" owner:self options:nil];
        HomePageWaterfall *homePageWaterfall = nib[0];
        
        //判断是左边还是右边
        int Y;
        BOOL BOOLY;
        if (y<y1) {
            BOOLY = YES;
            Y = y;
        }
        else
        { BOOLY = NO;
            Y = y1;
        }
        int X = (y<y1)?0:delegate.width/2;
        
        
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"fushi%@.jpg",picmutArray[i]]];
        [homePageWaterfall ViewframSetX:X SetY:Y Pic:image delegateW:delegate.width];
        
        if(BOOLY)
            y += homePageWaterfall.frame.size.height;
        else
            y1 += homePageWaterfall.frame.size.height;
        //        waterView.backgroundColor = [UIColor redColor];
        //        waterView.alpha = 0.5;
        
        [self.scrollview addSubview:homePageWaterfall];
        
    }
    
    AllY =+ (y<y1)?y1:y;
    self.scrollview.contentSize = CGSizeMake(delegate.width, AllY);
    
    
}


@end
